package com.own.component.common.mvc;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.own.component.common.util.serializer.CustomZonedDateTimeSerializer;
import com.own.constant.ConstantDateFormatPattern;

import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

/**
 * JacksonObjectMapper
 *
 * @author chenxueli
 * @date 2022-10-21 23:07:00
 */
@SuppressWarnings("all")
public class JacksonObjectMapper extends ObjectMapper {

    public JacksonObjectMapper() {
        super();
        // 收到未知属性时不报异常
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

        // 反序列化时，属性不存在的兼容处理
        this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        var simpleModule = new SimpleModule()
                .addSerializer(BigInteger.class, ToStringSerializer.instance)
                .addSerializer(Long.class, ToStringSerializer.instance);


        var javaTimeModule = new JavaTimeModule()
                .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(ConstantDateFormatPattern.Formatter.DATETIME))
                .addDeserializer(LocalDate.class, new LocalDateDeserializer(ConstantDateFormatPattern.Formatter.DATE))
                .addDeserializer(LocalTime.class, new LocalTimeDeserializer(ConstantDateFormatPattern.Formatter.HH_MM_SS))
                .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(ConstantDateFormatPattern.Formatter.DATETIME))
                .addSerializer(ZonedDateTime.class, new CustomZonedDateTimeSerializer(ConstantDateFormatPattern.Formatter.DATETIME_SSS))
                .addSerializer(LocalDate.class, new LocalDateSerializer(ConstantDateFormatPattern.Formatter.DATE))
                .addSerializer(LocalTime.class, new LocalTimeSerializer(ConstantDateFormatPattern.Formatter.HH_MM_SS));

        // 注册功能模块 例如，可以添加自定义序列化器和反序列化器
        this.registerModules(new Jdk8Module(), simpleModule, javaTimeModule);
    }

    public static void main(String[] args) throws JsonProcessingException {
        var mapper = new JacksonObjectMapper();
        System.out.println(mapper.writeValueAsString(LocalDateTime.now()));
        System.out.println(mapper.writeValueAsString(ZonedDateTime.now()));
        System.out.println(ZonedDateTime.now().format(ConstantDateFormatPattern.Formatter.DATETIME_SSS));
    }

}
